﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
using System.Data.SqlClient;

public partial class vote_VoteShow : System.Web.UI.Page
{
    //public string userid = "";
    public string title = "";
    public string desc = "";
    public string myAnswer = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["id"] != null)
            Bind();
        if (!IsPostBack)
        {
            if ((this.Session["ecrm_email"] == null) || (this.Session["ecrm_email"].ToString() == ""))
            {
                userstatus1.Text = "你好，请 <a href=\"main.aspx\">登录</a>或 ";
                userstatus2.Text = "<a href=\"../user/user_reg.aspx\">注册</a>";
            }
            else
            {
                string sid = this.Session["ecrm_uid"].ToString();
                TUser oUser = new TUser(sid);
                oUser.Select();
                string bigname = oUser.BigName;

                userstatus1.Text = "<a onclick='javascript:location.href=\"../user/user_myself.aspx\"' onmouseover='this.style.cursor=\"pointer\"' onmouseout='this.style.backgroundColor=\"\"'>" + bigname + "的设置</a>";
                userstatus2.Text = "<a onclick='javascript:location.href=\"../user/user_logout.aspx\"' onmouseover='this.style.cursor=\"pointer\"' onmouseout='this.style.backgroundColor=\"\"'>退出</a>";
            }
        }
    }

    public void Bind()
    {
        TVote oVote=new TVote(Request["id"].ToString());
        oVote.Select();
        title = "主题：" + oVote.Title;
        desc = "描述：" + oVote.Description;
        DataTable dt=new DataTable();
        dt.Columns.Add(new DataColumn ("tr"));

        string sSql = "select * from t_vote_item where voteid='" + oVote.Id + "' and voteid not in ( select voteid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        SqlDataReader drritem=myReader.FDataReader;
        if (drritem.HasRows)
        {
            DataRow dr=dt.NewRow();
            string div="";
            while (drritem.Read())
            {
                if (oVote.Type == 1)
                    div += "<tr><td><input type='radio' name='" + oVote.Id  + "' value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
                else
                    div += "<tr><td><input type='checkbox' name='" + oVote.Id  + "'value='" + drritem["id"].ToString() + "' />" + drritem["content"].ToString() + "</td></tr>";
            }
            div += "<tr><td><input type='button' onclick=javascript:vote('" + oVote.Id  + "') value='投票'></td></tr>";
            dr["tr"]=div;
            dt.Rows.Add(dr);
        }


        sSql = " select * from t_vote_item where voteid='" + oVote.Id + "' and voteid in ( select voteid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
        int total = Convert.ToInt32(TSqlDB.ExecuteScalar("select isnull(sum(votes),0) from t_vote_item where voteid='" + oVote.Id + "' and voteid in ( select voteid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )"));
        myReader = new TSqlDBReader(sSql);
        drritem=myReader.FDataReader;
        while (drritem.Read())
        {
            DataRow dr=dt.NewRow();
            float rate = float.Parse(drritem["votes"].ToString()) / total * 100;
            if (rate == 0)
            {
                dr["tr"] = "<tr><td>" + drritem["content"].ToString() + "：</td><td><div class='chartdiv-bk'><div title='" + Math.Round(rate, 0).ToString() + "' style='overflow:hidden;background-color:Red;height:7px;position:absolute;'></div></div></td><td>" + Math.Round(rate, 0).ToString() + "%(" + drritem["votes"].ToString() + ")人</td></tr>";
            }
            else
            {
                dr["tr"] = "<tr><td>" + drritem["content"].ToString() + "：</td><td><div class='chartdiv-bk'><div class='test' title='" + Math.Round(rate, 0).ToString() + "' style='overflow:hidden;background-color:Red;height:7px;position:absolute;'></div></div></td><td>" + Math.Round(rate, 0).ToString() + "%(" + drritem["votes"].ToString() + ")人</td></tr>";
            }
            dt.Rows.Add(dr);
        }

        sSql = " select * from t_vote_item where voteid='" + oVote.Id + "' and id in ( select voteitemid from t_vote_user where userid ='" + Session["userid"].ToString() + "' )";
        myReader = new TSqlDBReader(sSql);
        drritem = myReader.FDataReader;
        myAnswer += "<table width='100%'>";
        while (drritem.Read())
        {
            myAnswer += "<tr><td  align='center'>" + drritem["content"].ToString() + "</td></tr>";
        }
        myAnswer += "</table>";

        rpitems.DataSource=dt;
        rpitems.DataBind();
        drritem.Close();
        myReader.Free();

    }
}
